package test;

import java.util.Random;

public class MineUtils {
  public static void main(String[] args) {
    int[] arr = shuffle(intArr(16));
    for (int i = 0; i < arr.length; i++) {
      System.out.print(arr[i] + " ");
    }

    System.out.println();

    int[][] twoArr = toTwoArray(arr, 4);

    for (int i = 0; i < twoArr.length; i++) {
      for (int j = 0; j < twoArr[i].length; j++) {
        System.out.print(twoArr[i][j] + " ");
      }
      System.out.println();
    }
  }

  // 初始化数组
  private static int[] intArr(int n) {
    int[] arr = new int[n];
    for (int i = 0; i < n; i++) {
      arr[i] = i;
    }
    return arr;
  }

  // 打乱数组
  private static int[] shuffle(int[] arr) {
    Random random = new Random();

    for (int i = 0; i < arr.length; i++) {
      int index = random.nextInt(arr.length);
      int temp = arr[i];
      arr[i] = arr[index];
      arr[index] = temp;
    }

    return arr;
  }

  // 将数组成员放入二维数组中
  private static int[][] toTwoArray(int[] arr, int num) {
    int[][] data = new int[num][num];
    for (int i = 0; i < data.length; i++) {
      for (int j = 0; j < data[i].length; j++) {
        data[i][j] = arr[i * 4 + j];
      }
    }
    return data;
  }
}
